import { request } from "../../utils/request.js";
import regeneratorRuntime from "../../utils/runtime.js";
var bsurl = require('../../utils/bsurl.js');
let flashEvent = require('../../utils/FlashEvent.js')
let  app =getApp();
Page({
  data: {
    zzPlay:false,
    animation:'paused',
    playListId:0,
    albumListId:0,
    playIdList:[],
    playlist:[],
    playingId:0,
    playerData:{},
    musicInfo:{},
    playingName:'播放页面',
    isplay:false,
    duration: '00:00',
    playtime: '00:00',
    playingTime:0,
    PlayWay:'../../icon/music_play/cm2_icn_loop@2x.png',
    playwaynum:1,
    isshuffle:false,
    isloop:false,
    playlist_Zindex:0,
    playRelevant_Zindex:0,
  },

  onLoad: function (options){
    this.watchPlayerData(this.watchBackPlayerData)
    const playListId=options.playListId;
    const albumListId=options.aid;
    const dailySongId=options.dailySongId;
    const playingId=options.playId;
    this.setData({
      albumListId,
      playListId,
      playingId,
    });
    if(dailySongId){
      this.getdailySonge();
    }
    else if(playListId!=0&&albumListId==0){
      this.handlePlaylist();
    }
    else if(albumListId!=0&&playListId==0){
      this.getalbum();
    }
    else{
      this.setData({
        playingId,
        playIdList:[playingId]
      });
    }
    this.hanlePlayinginfo();
  },
  //获取当前播放歌曲图片和名字 
  async hanlePlayinginfo(){
    const id=this.data.playingId
    const playinfo=await request({url: bsurl+'song/detail?ids='+id});
    let musicInfo={
      playingImg:playinfo.songs[0].al.picUrl,
      playingName:playinfo.songs[0].name,
      playingAlbumName:playinfo.songs[0].al.name,
      playingAlbumId:playinfo.songs[0].al.id,
      playingAuthor:playinfo.songs[0].ar[0].name,
      playingAuthorId:playinfo.songs[0].ar[0].id,
    }
    this.setData({
      musicInfo
    })
    wx.setNavigationBarTitle({
      title: playinfo.songs[0].name
    })
  },
  //如果是每日歌单
  async getdailySonge(){
    const result=await request({url: 'http://47.106.223.193:3000/v1/recommend/songs'}); 
    let playIdList=result.recommend.map(v=>v.id)
    let playlist=result.recommend.map(v=>{
      return {
        id:v.id,
        name:v.name,
        author:v.artists[0].name,
      }
    })
    this.setData({
      playIdList,
      playlist
    })
  },

  //从歌单id获取歌曲列表id
  async handlePlaylist(){
    const {playListId}=this.data;
    const playlistinfo=await request({url: bsurl+'playlist/detail?id='+playListId});
    const playlist=playlistinfo.playlist.tracks.map(v=>{
      return {
        id:v.id,
        name:v.name,
        author:v.ar[0].name
      }
    });
    const playIdList=playlistinfo.privileges.map(v=>v.id)
    this.setData({
      playIdList,
      playlist
    })
  },
  //如果是专辑就获取专辑信息
  async getalbum(){
    const {albumListId}=this.data;
    const playlistinfo=await request({url: bsurl+'album?id='+albumListId});
    const playlist=playlistinfo.songs.map(v=>{
      return {
        id:v.id,
        name:v.name,
        author:v.ar[0].name
      }
    });
    const playIdList=playlistinfo.songs.map(v=>v.id)
    this.setData({
      playIdList,
      playlist
    })
  },

  onReady: function (e) {
    let {playingId}=this.data;
    let playingIdd=app.globalData.playingId;
    //console.log(playingIdd);
    if(playingId==playingIdd){
      //console.log("hanser")
      this.setData({
        zzPlay:app.globalData.zzPlay,
        animation:app.globalData.animation,
        isplay:app.globalData.zzPlay,
      })
    }else{
      getApp().globalData.playingId=playingId;
    }
    let that = this;
    getApp().watchdata(that.watchBackPlaystate)
    getApp().watchonEnded(that.watchBackonEnded)
    getApp().watchIsplay(that.watchBackIsplay)
  },
  //监听播放id
  watchPlayerData:function(method){
    var obj = this.data;
    Object.defineProperty(obj,"playingId",
    {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this._name = value;
        method(value);
      },
      get:function(){
        return this._name
      }
    })
  }, 
  watchBackPlayerData: function (value){
    //给小播放器填充数据
    let playerData={
        isplayer:true,
        playingId:this.data.playingId,
        playListId:this.data.playListId,
        albumListId:this.data.albumListId
      }
    //发送数据
    flashEvent.post(flashEvent.EVENT_KEYS.Id,playerData);
    getApp().globalData.playerData=playerData
  },
  //监听播放状态的回调函数
  watchBackPlaystate: function (value){
    this.setData({
      duration:value.duration,
      playtime:value.playtime,
      playingTime:value.playingTime
    });
  },
  //播放完毕回调函数
  watchBackonEnded: function (vlue){
    this.handleIsshuffle()
  },
  //判断是否随机
  handleIsshuffle(){
    let {isshuffle}=this.data;
    let {isloop}=this.data;
    //判断是否随机
    if(isloop){
      app.isloop()
    }
    else{
      if(isshuffle){
        this.shuffle();
      }
      else{
        this.nextPlay();
      }
    }
  },
  //播放状态回调函数
  watchBackIsplay: function (value){
    if(value){
      this.setData({
        isplay:true,
        zzPlay:true,
        animation:'running'
      });
    }
    else{
      this.setData({
        isplay:false,
        zzPlay:false,
        animation:'paused'
      });
    }
    flashEvent.post(flashEvent.EVENT_KEYS.isplayer,value)
  },
  //操作事件逻辑
  //播放方式
  hanlePlayWay(){
    let {PlayWay}=this.data;
    let {playwaynum}=this.data;
    if(playwaynum==1){
      playwaynum+=1;
      PlayWay='../../icon/music_play/cm2_icn_shuffle@2x.png'; 
      wx.showToast({
        title: '随机播放',
        icon: 'success',
      });
      this.setData({
        isshuffle:true,
        isloop:false
      })
    }
    else if (playwaynum==2){
      playwaynum+=1;
      PlayWay='../../icon/music_play/cm2_icn_one@2x.png';
      wx.showToast({
        title: '单曲循环',
        icon: 'success',
      });
      this.setData({
        isshuffle:false,
        isloop:true
      })
    }
    else{
      playwaynum=1
      PlayWay='../../icon/music_play/cm2_icn_loop@2x.png';
      wx.showToast({
        title: '列表循环',
        icon: 'success',
      });
      this.setData({
        isshuffle:false,
        isloop:false
      })
    }
    this.setData({
      PlayWay,
      playwaynum
    })
  },
  //随机播放事件
  shuffle(){
    let {playingId}=this.data;
    let {playIdList}=this.data;
    let length=playIdList.length;
    if(length==1){
      playingId=playIdList[0];
    }else{
      do{
        var index=this.GetRandomNum(0,length);
      }
      while ( (playingId==playIdList[index])||(index>=playIdList.length) ) 
      playingId=playIdList[index]
    }
    this.setData({
      playingId
    })
    this.hanlePlayinginfo()
    getApp().globalData.playingId =playingId;
  },
  //随机函数
  GetRandomNum(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return(Min + Math.round(Rand * Range));
  },
  //暂停与播放事件
  handleAudioPlay (){
    app.handleAudioPlay()
  },
  //上一首歌曲事件
  lastPlay(){
    let {isshuffle}=this.data;
    if(isshuffle){
      this.shuffle()
    }
    else{
      let {playingId}=this.data;
      let {playIdList}=this.data;
      let length=playIdList.length;
      let index = playIdList.findIndex(v=>v==playingId);
      if(index==0){
        playingId=playIdList[length-1]
        this.setData({
          playingId
        })
      }
      else{
        playingId=playIdList[index-1]
        this.setData({
          playingId
        })
      }

      this.hanlePlayinginfo()
      getApp().globalData.playingId =playingId;
    }
  },
  //下一曲事件
  nextPlay(){
    let {isshuffle}=this.data;
    if(isshuffle){
      this.shuffle()
    }
    else{
      let {playingId}=this.data;
      let {playIdList}=this.data;
      let length=playIdList.length;
      let index = playIdList.findIndex(v=>v==playingId);
      if(index==length-1){
        playingId=playIdList[0]
        this.setData({
          playingId
        })
      }
      else{
        playingId=playIdList[index+1]
        this.setData({
          playingId
        })
      }
      this.hanlePlayinginfo()
      getApp().globalData.playingId =playingId;
    }
  },
  //开启详情
  playRelevantOpen(){
    this.setData({
      playRelevant_Zindex:10
    })
  },
  //关闭详情
  playRelevantClose(){
    this.setData({
      playRelevant_Zindex:0
    })
  },
  //开启列表
  playlistOpen(){
    this.setData({
      playlist_Zindex:10
    })
  },
  //关闭列表
  playlistClose(){
    this.setData({
      playlist_Zindex:0
    })
  },
  //点击歌曲切换事件
  handleswitch(e){
    let {playingId}=this.data
    let {id}=e.currentTarget.dataset;
    if(id!=playingId){    
      getApp().globalData.playingId =id;
      this.setData({
        playingId:id
      })
      this.hanlePlayinginfo()
    }
  }
})